//
//  BaseNavigationController.swift
//  微博
//
//  Created by mac on 15/12/22.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class BaseNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //添加自定义控件的时候,没有手势进行添加
        self.interactivePopGestureRecognizer?.delegate = self
        
    }
    //重写push控制器的方法
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        
        //当有child的时候,就会出现自定义的item
        if self.childViewControllers.count == 1 {
        //添加自定义的item按钮
        
        let backBtn = UIButton(title: "返回", imageName: "navigationbar_back_withtext", color: UIColor.randomColor(), fontSize: 14, backImageName: nil)
        backBtn.addTarget(self, action: "back", forControlEvents: .TouchUpInside)
        let backItem = UIBarButtonItem(customView: backBtn)
        viewController.navigationItem.leftBarButtonItem = backItem
        
        viewController.hidesBottomBarWhenPushed = true;
        }
        //调用父类的方法
        super.pushViewController(viewController, animated: animated)
    }
    
    @objc private func back() {
        self.popViewControllerAnimated(true)
    }
    
   
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
extension BaseNavigationController:UIGestureRecognizerDelegate {
    
    //添加手势
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        //当首页的时候,就不添加手势
        return self.childViewControllers.count != 0
    }
    
}
